import router from "@src/router";
import {
  PROJECT_TYPE,
  RESOURCES_TYPE,
  OPEN_STATE,
} from "@src/sp-reservation/constant/index.js";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import reservation from "@src/store/reservation";
import base from "@src/store/base";
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ["themeColor", "themeTitleColor", "perms", "businessName"],
    },
    {
      store: reservation,
      fields: ["resourcesDetail"],
      actions: ["setResourcesDetail"],
    },
  ],
  properties: {
    list: {
      type: Array,
      value: [],
    },
    tabsActived: {
      type: Number,
      value: PROJECT_TYPE,
    },
    shopId: {
      type: Number,
      value: 0,
    },
    shopConfigId: {
      type: Number,
      value: 0,
    },
    canScrollY: {
      type: Boolean,
      value: false,
    },
    isScrollToTop: {
      type: Boolean,
      value: false,
    },
    status: {
      type: Number,
      value: 0,
    },
  },
  data: {
    PROJECT_TYPE,
    RESOURCES_TYPE,
    originScrollTop: 0,
    supplementArrLength: 0,
    OPEN_STATE,
  },
  observers: {
    list: function (arr) {
      if (Array.isArray(arr)) {
        this.setData({
          supplementArrLength: arr.length < 10 ? 10 - arr.length : 0,
        });
      }
    },
  },
  methods: {
    onScrollToUpper() {
      console.log("列表到顶部", this.data.originScrollTop);
      this.triggerEvent("resetPage");
      if (this.data.originScrollTop < 10) {
        wx.nextTick(() => {
          this.triggerEvent("changeShowShopInfo", {
            isToTop: false,
          });
        });
      }
    },
    onScrollToLower() {
      this.triggerEvent("nextPage");
    },
    onScrollList(e) {
      const { scrollTop } = e.detail;
      const { originScrollTop } = JSON.parse(JSON.stringify(this.data));
      wx.nextTick(() => {
        if (scrollTop > 50 && scrollTop > originScrollTop) {
          this.triggerEvent("changeShowShopInfo", {
            isToTop: scrollTop > originScrollTop,
          });
        }
      });

      // console.log("在滚动", scrollTop);
      wx.nextTick(() => {
        this.setData({
          originScrollTop: scrollTop,
        });
        if (scrollTop < 50) {
          wx.nextTick(() => {
            this.triggerEvent("changeShowShopInfo", {
              isToTop: false,
            });
          });
        }
      });
    },
    handleToProjectDetail(e) {
      const {
        currentTarget: {
          dataset: { item },
        },
      } = e;
      //项目的时候
      const routerName =
        this.data.tabsActived === PROJECT_TYPE
          ? "project_detail" //项目详情
          : "resources_detail"; //资源详情
      if (routerName === "resources_detail") {
        this.setResourcesDetail(JSON.stringify(item));
      }
      router.push({
        name: `reservation/${routerName}`,
        params: {
          id: item.id,
          planId: item.id,
          shopId: this.data.shopId,
          shopConfigId: this.data.shopConfigId,
          goodsSerial: item.goodsSerial,
        },
      });
    },
  },
});
